//
//  YbrDownDao.m
//  wifibox
//
//  Created by os X 10.9 on 14-12-19.
//  Copyright (c) 2014年 yinbaner. All rights reserved.
//

#import "YbrDownDao.h"
//#import "YbrDownItem.h"

@implementation YbrDownDao

-(BOOL) setDownOver:(NSString *)name downTime:(NSString *) time musicId:(int) musicId {
    __block BOOL isOk = false;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdate:@"insert into down_over (name ,music_id, downTime) VALUES (?,?,?)", name ,[NSNumber numberWithInt:musicId],time];
    }];
    return isOk;
}

-(BOOL) removeDownOver:(int) musicId {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from down_over where music_id=%i",musicId];
    }];
    
    return isOk;
}

-(NSArray *) getDownOverlist {
    __block NSArray *arr;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select distinct name ,downTime ,music_id from down_over order by _id DESC"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
//            YbrDownItem *item = [[YbrDownItem alloc] init];
//            item.name = [rs stringForColumn:@"name"];
//            item.downTime = [rs stringForColumn:@"downTime"];
//            item.musicId = [rs intForColumn:@"music_id"];
//            [tempArr addObject:item];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    return arr;
}

-(NSArray *) getMusicListByIds:(NSArray *) musicIds {
    __block NSArray *musiclist;
    [[YbrSQLHelper getDataBase] inTransaction:^(FMDatabase *db, BOOL *rollback) {
        FMResultSet *rs;
        NSMutableArray *tempArr = [NSMutableArray array];
        
        for (int i = 0; i < musicIds.count; i ++) {
            // 1.查询数据
            rs = [db executeQueryWithFormat:@"select distinct musicid ,name, singername ,albumname ,downloadurl ,file  ,img ,duration ,type ,timerstr from local_music where musicid=%i" , [musicIds[i] intValue]];
            
            if(rs.next) {
                do {
                    YbrMusicItem *musicItem = [[YbrMusicItem alloc] init];
                    musicItem.musicId = [rs intForColumn:@"musicid"];
                    musicItem.musicName = [rs stringForColumn:@"name"];
                    musicItem.singername = [rs stringForColumn:@"singername"];
                    musicItem.albumname = [rs stringForColumn:@"albumname"];
                    musicItem.downloadurl = [rs stringForColumn:@"downloadurl"];
                    musicItem.file = [rs stringForColumn:@"file"];
                    musicItem.img = [rs stringForColumn:@"img"];
                    musicItem.duration = [rs intForColumn:@"duration"];
                    musicItem.type = [rs intForColumn:@"type"];
                    musicItem.timeStr = [rs stringForColumn:@"timerstr"];
                    
                    [tempArr addObject:musicItem];
                }while (rs.next);
            }else { // 如果是这样的话就再找找本地课程的是否存在
                rs = [db executeQueryWithFormat:@"select distinct table1.musicid musicid ,table1.name name  ,table1.downloadurl downloadurl ,table1.file file ,table1.timerstr timerstr ,table1.duration duration ,table2.name coursename ,table2.channelname channelname from local_course_Music table1 left join local_course table2 on table1.courseid = table2.courseid where table1.musicid=%i" ,[musicIds[i] intValue]];
                
                if(rs.next) {
                    do {
                        YbrMusicItem *item = [[YbrMusicItem alloc] init];
                        
                        item.musicId = [rs intForColumn:@"musicid"];
                        item.musicName = [rs stringForColumn:@"name"];
                        item.downloadurl = [rs stringForColumn:@"downloadurl"];
                        item.file = [rs stringForColumn:@"file"];
                        item.timeStr = [rs stringForColumn:@"timerstr"];
                        item.duration = [rs intForColumn:@"duration"];
                        item.type = Type_Http_Source;
                        item.singername = [rs stringForColumn:@"coursename"];
                        item.albumname = [rs stringForColumn:@"channelname"];
                        
                        [tempArr addObject:item];

                    } while (rs.next);
                } else {
                    rs = [db executeQueryWithFormat:@"select distinct table1.musicid musicid ,table1.name name  ,table1.downloadurl downloadurl ,table1.file file ,table1.timerstr timerstr ,table1.duration duration ,table2.name coursename ,table2.channelname channelname from local_second_course_Music table1 left join local_course table2 on table1.courseid = table2.courseid where table1.musicid=%i" ,[musicIds[i] intValue]];
                    while (rs.next) {
                        YbrMusicItem *item = [[YbrMusicItem alloc] init];
                        
                        item.musicId = [rs intForColumn:@"musicid"];
                        item.musicName = [rs stringForColumn:@"name"];
                        item.downloadurl = [rs stringForColumn:@"downloadurl"];
                        item.file = [rs stringForColumn:@"file"];
                        item.timeStr = [rs stringForColumn:@"timerstr"];
                        item.duration = [rs intForColumn:@"duration"];
                        item.type = Type_Http_Source;
                        item.singername = [rs stringForColumn:@"coursename"];
                        item.albumname = [rs stringForColumn:@"channelname"];
                        
                        [tempArr addObject:item];
                    }
                }
                
            }
        }
        
        musiclist = [tempArr copy];
        [rs close];
    }];
    return musiclist;
}

// 是否已经下载
-(BOOL) isDown:(int) musicId {
    __block BOOL isOk = NO;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        rs = [db executeQueryWithFormat:@"select music_id from down_over where music_id=%i",musicId];
        if(rs.next) {
            isOk = YES;
        }
        [rs close];
    }];
    return isOk;
}

-(NSArray *) isDownByIds:(NSArray *)musicIdArr {
    __block NSMutableArray *haveDownList = [NSMutableArray array];
    
    [[YbrSQLHelper getDataBase] inTransaction:^(FMDatabase *db, BOOL *rollback) {
        FMResultSet *rs;
        
        for (int i = 0; i < musicIdArr.count; i ++) {
            rs = [db executeQueryWithFormat:@"select music_id from down_over where music_id=%i",[musicIdArr[i] intValue]];
            if(rs.next) {
                [haveDownList addObject:[NSNumber numberWithInt:i]];
            }
            [rs close];
        }
    }];
    
    return haveDownList;
}

@end
